﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zhuang.WinForm.AppCode.Common;
using Zhuang.Utility.Common.DataBase;

namespace Zhuang.WinForm.AuthorityManager
{
    public partial class UserAddOrUpdate : Form
    {
        public int userid = -1;
        public string userName = "";
        public string userPassowrd = "";

        public UserAddOrUpdate()
        {
            InitializeComponent();
        }

        private void UserAddOrUpdate_Load(object sender, EventArgs e)
        {
            if (userid == -1)
            {
                this.Text = "添加用户";
            }
            else
            {
                this.Text = "修改用户";
                this.tbUserName.Text = userName;
                this.tbUserPassword.Text = userPassowrd;


            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            userName = tbUserName.Text.Trim();
            userPassowrd = tbUserPassword.Text.Trim();

            if (userName == "")
            {
                MessageBox.Show("用户名不能为空！");
            }
            else
            {
                try
                {
                    string strSql = "";

                    if (userid == -1)
                    {
                        strSql = "insert into App_User(username,userpassword) values('" + userName + "','" + userPassowrd + "')";
                    }
                    else {
                        strSql = "update App_User set username='"+userName+"',userpassword='"+userPassowrd+"' where userid="+userid.ToString();
                    }


                    
                    SqlHelper.ExecuteSql(strSql);

                    MessageBox.Show("保存成功！");
                    this.Close();
                    this.DialogResult = DialogResult.OK;

                }
                catch (Exception ex)
                {
                    MessageBox.Show("保存失败，请检用户名是否已存在！" + ex.Message);

                }

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.Cancel;

        }

        private void tbUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tbUserPassword.Focus();
            }
        }

        private void tbUserPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOk.Focus();
            }
        }
    }
}
